package com.itheima.tanhua.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.tanhua.pojo.db.Admin;
import com.itheima.tanhua.utils.AppJwtUtil;
import com.itheima.tanhua.vo.AdminUserInfoVo;
import com.itheima.tanhua.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private HttpServletResponse response;

    //用户登录验证码图片
    public void verification(String uuid) throws IOException {
        //1.生成验证码  CaptchaUtil验证码生成工具
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(299, 97);
        String code = circleCaptcha.getCode();
        //2.将验证码存入redis中
        String redisKey = "admin_" + uuid;
        stringRedisTemplate.opsForValue().set(redisKey, code);
        //将验证码以流的形式写入前端
        circleCaptcha.write(response.getOutputStream());
    }

    //登录后台系统
    public ResponseEntity login(Map<String, String> map) {
        //从map中获取username  password  verificationCode  uuid
        String username = map.get("username");
        String pwd = map.get("password");
        String password = SecureUtil.md5(pwd);//md5加密后的密码
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        if (username == null || pwd == null) {
            //用户名或密码为空
            throw new RuntimeException("用户名或密码为空,请重新输入");
        }
        //1.根据用户名查询数据库,并返回对象
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername, username);
        Admin admin = adminMapper.selectOne(wrapper);
        //判断用户是否存在如果是新用户,不存在添加用户
        if (ObjectUtil.isEmpty(admin)){
            //新用户,新用户添加
//            admin.setUsername(username);
//            admin.setPassword(password);
//            admin.setAvatar();
            throw new RuntimeException("没有此用户,请先注册再登录");
        }
        //比教用户的密码是否相同 密码MD5加密的
        if (!StrUtil.equals(admin.getPassword(), password)) {
            //密码不正确
            throw new RuntimeException("您的密码不正确,请重新登录");
        }
        //从redis中获取code验证码
        String redisKey = "admin_" + uuid;
        String code = stringRedisTemplate.opsForValue().get(redisKey);
        //销毁redis的key
        stringRedisTemplate.delete(redisKey);
        //比较验证码是否相等
        if (!StrUtil.equals(code, verificationCode)) {
            throw new RuntimeException("验证码输入错误");
        }
        //获取token的值
        //获取用户的id
        Long uid = admin.getId();
        String token = AppJwtUtil.getToken(uid);
        //将token返回
        Map parem=new HashMap();
        parem.put("token",token);
        return ResponseEntity.ok(parem);
    }


    //用户基本信息
    public AdminUserInfoVo adminUserInfo() {
        //获取存在token中的de uid
        //获取当前用户的id uid
        String redisKey = "CHECK_ID";
        Long uid = Convert.toLong(stringRedisTemplate.opsForValue().get(redisKey));
        //获取用户信息  封装返回值
        Admin admin = adminMapper.selectById(uid);
        AdminUserInfoVo adminUserInfoVo=new AdminUserInfoVo();
        adminUserInfoVo.setId(admin.getId().toString());
        adminUserInfoVo.setUsername(admin.getUsername());
        adminUserInfoVo.setAvatar(admin.getAvatar());
        return adminUserInfoVo;

    }

    //登出系统
    public ResponseEntity logout() {
        //登出系统 删除存入的token
        return ResponseEntity.ok(null);
    }
}
